﻿<?php
	$path = "../";
	include_once($path.'configuration.php');
	/*Traitement*/
	include_once($path.'head.php');
	
	if(isset($_GET['action']))
		$action = $_GET['action'];
	else
		$action='add';
		
	if($action=="add"){
		if(isset($_POST) && isset($_POST['submit']) && $_POST['submit'] != ''){
			extract($_POST);
			if(isset($machine) && $machine !='' && isset($type) && $type !=''  && isset($resolution) && $resolution !='' ){
				$incident = new Incident($dbh);
				$incident->setMachine($machine);
				$incident->setType($type);
				$incident->setComplement($complement);
				$incident->setIs_resolved($resolution);				
				if($incident->save()){
					header('Location: '.$path . 'administration/gestion_incident.php');
					exit;
				}
			}else{
				$message = "Veuillez remplir tous les champs obligatoires !";
			}
		}
	}elseif($action == "modif"){
			if(isset($_GET) && isset($_GET['incident']) && $_GET['incident'] !=0){
				if(isset($_POST) && isset($_POST['submit']) && $_POST['submit'] != ''){
					extract($_POST);
					
					if(isset($machine) && $machine !='' && isset($type) && $type !=''  && isset($resolution) && $resolution !=''){
						$incident = new Incident($dbh, $_GET['incident']);
						$incident->setMachine($machine);
						$incident->setType($type);
						$incident->setComplement($complement);
						$incident->setIs_resolved($resolution);
						$incident->save();
							header('Location: '.$path .'administration/gestion_incident.php');
					}else{
						$message = "Veuillez remplir tous les champs obligation !";
					}
				}
			}
	}elseif($action == "delete"){
		if(isset($_GET) && isset($_GET['incident']) && $_GET['incident'] != 0){
			$incident = new Incident($dbh, $_GET['incident']);
			$incident->delete();
			header('Location: '.$path .'administration/gestion_incident.php');
		}
	}
	if($action == "modif"){
		if(isset($_GET) && isset($_GET['incident']) && $_GET['incident'] !=0){
			$incident = new Incident($dbh, $_GET['incident']);
			$machine = $incident->getMachine();
			$type = $incident->getType();
			$complement = $incident->getComplement();
			$date_detection = $incident->getDate_detection();
			$date_resolution = $incident->getDate_resolution();
			$resolved = $incident->getIs_resolved();
		}
	}else{
		$resolved = false;
	}
?>

<div id="bodyWrapper">
	<script type="text/javascript">
	$(function() {
		$('#form').submit(function(){
			var error = false;
			var message = 'Erreur : <br/>';
			
			if($("#machine").val() == ''){
				message += 'Choisissez une machine ! <br/>';
				$("#machine").addClass( "error_input" );
				error = true;
			}else{
				$("#machine").removeClass( "error_input" );
			}
			
			if($("#type").val() == '' || !bonip($("#ip").val())){
				message += 'Choisissez un type ! <br/>';
				$("#type").addClass( "error_input" );
				error = true;
			}else{
				$("#type").removeClass( "error_input" );
			}
			
			if($("#resolution").val() == '')){
				message += 'Le problème est-il résolu ? <br/>';
				$("#resolution").addClass( "error_input" );
				error = true;
			}else{
				$("#resolution").removeClass( "error_input" );
			}

			if(error == true){
				$("#dialog").html('<p>'+message+'</p>');
				$("#dialog").dialog("open");
				return false;
			}
		});
	});
	</script>
	<?php if(isset($message) && $message != ''){?>
	<script type="text/javascript" >
		$(function() {
			
			$("#modular_dialog").dialog({
				autoOpen: true,
				show: {
					effect: "explode",
					duration: 1000
				},
				hide: {
					effect: "explode",
					duration: 1000
				},
				modal: true
			});
			
		});
	</script>
	<div id="modular_dialog">
	<p><?php echo $message;?></p>
	</div>
	<?php } ?>
	<form method="post" id="form" action="">
		<p>
			<label for="machine" class="label">Machine* :</label>
			<select class="input" id="machine" name="machine">
				<?php 
					$machines = $dbh->getAllMachine();
					foreach($machines as $value){
				?>
				<option value="<?php echo $value['id_machine'];?>" <?php if(isset($machine) && $value['id_machine'] == $machine->getId()){
					echo 'selected="selected"';}?>>
					<?php echo $value['nom'];?>
				</option>
				<?php
					}
				?>
			</select>
		</p>
		<p>
			<label for="type" class="label">Type* :</label>
			<select class="input" id="type" name="type">
				<?php 
					$types = $dbh->getAllType_Incident();
					foreach($types as $value1){
				?>
				<option value="<?php echo $value1['id_type_incident'];?>" <?php if(isset($type) && $value1['id_type_incident'] == $type->getId()){
					echo 'selected="selected"';}?>>
					<?php echo $value1['nom_type'];?>
				</option>
				<?php
					}
				?>
			</select>
		</p>
		<p>
			<label for="complement" class="label">Complement :</label>
			<input type="text" id="complement" name="complement" class="input" value="<?php if(isset($complement)) echo $complement; ?>" />
		</p>
		<p>
			<label for="resolution" class="label">Resolu* :</label>
			<select name="resolution" id="resolution" class="input"> 
				<option value="true" <?php if($resolved == true){echo 'selected="selected"';} ?>>Oui</option>
				<option value="false" <?php if($resolved == false){echo 'selected="selected"';} ?>>Non</option>
			</select>
		</p>
		<p>
			<input type="submit" name="submit" id="submit" class="input" value="Envoyer" />
		</p>
		
		
	</form>
</div>
<?php
include_once($path.'footer.php');
?>